package com.tobeface.wechat.message.handler;

import com.google.common.base.Objects;
import com.tobeface.wechat.message.WeChatMessage;
import com.tobeface.wechat.message.WeChatMessageChannel;
import com.tobeface.wechat.message.WeChatMessagePayloads;
import com.tobeface.wechat.message.WeChatMessageTypes;

/**
 * @author loudyn
 */
public abstract class LinkMessageHandlerSupport extends WeChatMessageHandlerSupport {

    @Override
    protected final boolean isMessageTypeSupport(String msgType, WeChatMessage message) {
        if (!Objects.equal(msgType, WeChatMessageTypes.LINK)) {
            return false;
        }

        String title = (String) message.getPayloads().get(WeChatMessagePayloads.TITLE_PAYLOAD);
        String descr = (String) message.getPayloads().get(WeChatMessagePayloads.DESCR_PAYLOAD);
        String url = (String) message.getPayloads().get(WeChatMessagePayloads.URL_PAYLOAD);
        return isLinkMessageSupport(message, title, descr, url);
    }

    /**
     *
     * @param message
     * @param title
     * @param descr
     * @param url
     * @return
     */
    protected boolean isLinkMessageSupport(WeChatMessage message, String title,
                                           String descr, String url) {
        return true;
    }

    @Override
    protected final void onMessageReceived(WeChatMessageChannel channel, WeChatMessage message) {
        String title = (String) message.getPayloads().get(WeChatMessagePayloads.TITLE_PAYLOAD);
        String descr = (String) message.getPayloads().get(WeChatMessagePayloads.DESCR_PAYLOAD);
        String url = (String) message.getPayloads().get(WeChatMessagePayloads.URL_PAYLOAD);
        onLinkMessageReceived(channel, message, title, descr, url);
    }

    /**
     *
     * @param channel
     * @param message
     * @param title
     * @param descr
     * @param url
     */
    protected abstract void onLinkMessageReceived(WeChatMessageChannel channel, WeChatMessage message,
                                                  String title, String descr, String url);
}
